package com.toro.gdd2011.onepersongame;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;

public class Game {

	private class GameData {
		int mNum;
		int mData[];
	}
	
	public static void main(String[] args) {
		new Game().run();
	}

	private int mGameCount;
	private GameData mGame[];
	private int mResult[];

	private void run() {
		// ファイルの読み込み
		readFile();
		// 中間データ、解答データ等の初期化
		init();
		// 解く
		solve();
		// ファイルに書き出し
		writeFile();
	}

	private void readFile() {
		try{
			BufferedReader br = new BufferedReader(new FileReader("./input.dat"));
			String[] line;
			// 問題数の読み込み
			mGameCount = Integer.valueOf(br.readLine());
			mGame = new GameData[mGameCount];
			for(int i = 0; i < mGameCount; i++) {
				mGame[i] = new GameData();
				mGame[i].mNum = Integer.valueOf(br.readLine());
				mGame[i].mData = new int[mGame[i].mNum];
				line = br.readLine().split(" ");
				for(int j = 0; j < mGame[i].mNum; j++) {
					mGame[i].mData[j] = Integer.valueOf(line[j]);
				}
			}
			// 読み込み終了
			br.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	private void init() {
		mResult = new int[mGameCount];
	}

	private void solve() {
		for(int i = 0; i < mGameCount; i++) {
			mResult[i] = solve(mGame[i]);
		}
	}

	private int solve(GameData gameData) {
		if(isSolve(gameData)) {
			return 0;
		}
		// 下1桁が5の数
		int cntFive = 0;
		// 下1桁が0の数
		int cntZero = 0;
		// 残っているデータ数
		int cntRemaining = 0;
		
		for(int i = 0; i < gameData.mNum; i++) {
			if(gameData.mData[i] == -1) {
				continue;
			}
			if(gameData.mData[i] % 10 == 5) {
				cntFive++;
			}else if(gameData.mData[i] % 10 == 0) {
				cntZero++;
			}
			cntRemaining++;
		}
		int numRemoveFiveTimesData = -1;
		int numHalvData = -1;
		if(cntFive > 0 || cntZero == cntRemaining) {
			// 5の倍数を削除した時の解
			GameData copyDataForFiveTimes = copyGameData(gameData);
			RemoveFiveTimesData(copyDataForFiveTimes);
			numRemoveFiveTimesData = solve(copyDataForFiveTimes);
		}
		if(cntZero != cntRemaining) {
			// 半分にした時の解
			GameData copyDataForHalv = copyGameData(gameData);
			HalvData(copyDataForHalv);
			numHalvData = solve(copyDataForHalv);
		}
		boolean fiveTimes = false;
		if(numRemoveFiveTimesData != -1) {
			if(numHalvData == -1 || numRemoveFiveTimesData < numHalvData) {
				fiveTimes = true;
			}
		}
		return fiveTimes? numRemoveFiveTimesData + 1: numHalvData + 1;
	}

	private void RemoveFiveTimesData(GameData gameData) {
		for(int i = 0; i < gameData.mNum; i++) {
			if(gameData.mData[i] == -1) {
				continue;
			}
			if(gameData.mData[i] % 5 == 0) {
				gameData.mData[i] = -1;
			}
		}
	}
	
	private void HalvData(GameData gameData) {
		for(int i = 0; i < gameData.mNum; i++) {
			if(gameData.mData[i] == -1) {
				continue;
			}
			gameData.mData[i] /= 2;
		}
	}

	private boolean isSolve(GameData gameData) {
		for(int i = 0; i < gameData.mNum; i++) {
			if(gameData.mData[i] != -1) {
				return false;
			}
		}
		return true;
	}
	
	private GameData copyGameData(GameData gameData) {
		GameData copyData = new GameData();
		copyData.mNum = gameData.mNum;
		copyData.mData = new int[copyData.mNum];
		for(int i = 0; i < copyData.mNum; i++) {
			copyData.mData[i] = gameData.mData[i];
		}
		return copyData;
	}

	private void writeFile() {
		try{
			BufferedWriter bw = new BufferedWriter(new FileWriter("./output.dat"));
			for(int i = 0; i < mGameCount; i++) {
				bw.write(mResult[i] + "\n");
			}
			bw.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}

}
